package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.NotesService;
import net.taviscaron.drivenotes.shared.model.entity.Note;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class NotesActivity extends ListActivity {
    private static final String NOTE_ACTION = "net.taviscaron.drivenotes.android.NOTE";
    private NotesListAdapter adapter;
    private NotesService notesService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new NotesListAdapter();
        setListAdapter(adapter);
        getListView().setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		showNote(position);
	    }
	});
        
        notesService = new NotesService(this);
    }
    
    @Override
    protected void onStart() {
	super.onStart();
	if(adapter.getNotes() == null) {
	    if(notesService.isOnline()) {
		new NotesLoader().execute();
	    } else {
		Toast.makeText(this, "You need an internet connection", Toast.LENGTH_SHORT).show();
	    }
	}
    }
    
    private void showNote(int position) {
	Intent intent = new Intent(NOTE_ACTION);
	intent.putExtra("note", adapter.getItem(position));
	startActivity(intent);
    }
    
    private class NotesListAdapter extends BaseAdapter {
	private Note[] notes;
	
	public Note[] getNotes() {
	    return notes;
	}
	
	public void setNotes(Note[] notes) {
	    this.notes = notes;
	    notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
	    return (notes != null) ? notes.length : 0;
	}

	@Override
	public Note getItem(int position) {
	    return notes[position];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
	    if(view == null) {
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.notes_item, null);
	    }
	    
	    Note note = getItem(position);
	    ((TextView)view.findViewById(R.id.noteName)).setText(note.getName());

	    return view;
	}
    }
    
    private class NotesLoader extends AsyncTask<Void, Void, Note[]> {
	private ProgressDialog dialog;
	@Override
	protected Note[] doInBackground(Void... args) {
	    return notesService.loadNotes();
	}
	
	@Override
	protected void onPreExecute() {
	    dialog = ProgressDialog.show(NotesActivity.this, "", "Loading...");
	}
	
	@Override
	protected void onPostExecute(Note[] result) {
	    dialog.dismiss();
	    if(result == null || result.length == 0) {
		Toast.makeText(NotesActivity.this, "There is no notes", Toast.LENGTH_LONG).show();
	    } else {
		adapter.setNotes(result);
	    }
	}
    }
}
